package com.qdxwx.manage.controller.course;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.course.CourseFileService;

import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.course.CourseFile;
import com.qdxwx.models.manager.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 课程Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/course/file")
public class CourseFileController extends BaseController {

    @Autowired
    private CourseFileService courseFileService;

    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        CourseFile courseFile = new CourseFile();
        courseFile.setCourseId(courseId);
        List<CourseFile> list = courseFileService.getList(courseFile, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(courseFileService.count(courseFile));
        return rspData;
    }

    @GetMapping("/add.html")
    public String add(HttpServletRequest request, ModelMap mmap) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        if(courseId < 1) return error404;
        mmap.put("courseId",courseId);
        return "course/course-upload-file";
    }

    @PostMapping("/save.json")
    @ResponseBody
    public AjaxResult saveCourse(HttpServletRequest request) {
        Manager manager = SessionUtils.getManager();
        if(manager == null) return error("登录超时");
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        int fileSize = TKConvert.toInteger(request.getParameter("fileSize"));
        if(courseId < 1) return error("参数异常，缺少courseId");
        String fileUrl = request.getParameter("fileUrl");
        if(TKString.isEmpty(fileUrl)) return error("请上传或填写文件地址");
        String title = request.getParameter("title");
        if(TKString.isEmpty(title)) return error("文件名不能为空");
        CourseFile courseFile = new CourseFile();
        courseFile.setCourseId(courseId);
        courseFile.setSort(TKConvert.toInteger(request.getParameter("sort")));
        courseFile.setFileSize((fileSize/1024));//字节转换KB
        courseFile.setFileUrl(fileUrl);
        courseFile.setTitle(title);
        courseFile.setUserId(manager.getManagerId());
        courseFile.setUserName(manager.getManagerName());
        return toAjax(courseFileService.insert(courseFile));
    }

    /**
     * 删除
     */
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("ids"));
        if(id < 1) return error("参数异常");
        return toAjax(courseFileService.delete(id));
    }

}
